package pl.iachoo.ctl.tabory.dao.hibernate.services.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import pl.iachoo.ctl.tabory.dao.api.services.ITripService;
import pl.iachoo.ctl.tabory.dao.data.vo.TripVO;
import pl.iachoo.ctl.tabory.dao.hibernate.services.BaseDaoService;

public class TripDaoServiceImpl extends BaseDaoService<TripVO> implements
		ITripService {

	private final Logger logger = Logger.getLogger(TripDaoServiceImpl.class);

	public TripDaoServiceImpl(SessionFactory sessionFactory) {
		super(sessionFactory);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TripVO> getAllTrips() throws Exception {
		logger.debug("Getting all Trips");
		List<TripVO> listTrips = new ArrayList<TripVO>();
		Transaction transaction = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			transaction = session.beginTransaction();
			transaction.begin();
			listTrips = session.createCriteria(TripVO.class).list();
			transaction.commit();
			logger.debug(listTrips);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			transaction.rollback();
			throw e;
		} finally {
			session.close();
		}
		return null;
	}

	@Override
	public TripVO saveUpdateTrip(TripVO trip) throws Exception {
		logger.debug("Saving/updating Trip: " + trip);
		if (trip.getId() == null) {
			saveTrip(trip);
		} else {
			updateTrip(trip);
		}
		return null;
	}

	private void updateTrip(TripVO trip) throws Exception {
		logger.debug("Updating Trip: " + trip);
		Transaction transaction = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			transaction = session.beginTransaction();
			transaction.begin();
			session.update(trip);
			transaction.commit();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			transaction.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	private void saveTrip(TripVO trip) throws Exception {
		logger.debug("Saving Trip: " + trip);
		Transaction transaction = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			transaction = session.beginTransaction();
			transaction.begin();
			session.save(trip);
			transaction.commit();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			transaction.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@Override
	public void deleteTrip(TripVO trip) throws Exception {
		logger.debug("Deleting: " + trip);
		Transaction transaction = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			transaction = session.beginTransaction();
			transaction.begin();
			TripVO tmpTrip = (TripVO) session.get(TripVO.class, trip.getId());
			session.delete(tmpTrip);
			transaction.commit();
			logger.debug("Delete OK.");
		} catch (Exception e) {
			transaction.rollback();
			logger.error(e.getMessage(), e);
			throw e;
		} finally {
			session.close();
		}
	}

}
